<template>
  <basic-container>
    <avue-tabs :option="tabOption" @change="tabChange"></avue-tabs>
    <avue-crud
      :option="option"
      :table-loading="loading"
      :data="data"
      ref="crud"
      v-model="form"
      :before-open="beforeOpen"
      :before-close="beforeClose"
      :upload-before="uploadBefore"
      :upload-after="uploadAfter"
      @row-del="rowDel"
      @row-update="rowUpdate"
      @row-save="rowSave"
      @search-change="searchChange"
      @search-reset="searchReset"
      @current-change="currentChange"
      @size-change="sizeChange"
      @refresh-change="refreshChange"
      :page.sync="page"
      @on-load="onLoad"
    >
      <template v-slot:menu="{ size, disabled, type, row }">
        <el-button
          type="text"
          size="small"
          icon="el-icon-check"
          @click="check(row)"
          v-if="row.status === 'pending'"
          >通过
        </el-button>
        <el-button
          type="text"
          size="small"
          icon="el-icon-circle-close"
          @click="rejectApply(row)"
          v-if="row.status === 'pending'"
          >驳回
        </el-button>
      </template>
      <template v-slot:certificateFile="{ row }">
        <el-button
          size="mini"
          type="primary"
          @click="downloadFile(row.certificateFile)"
          icon="el-icon-download"
          >下载</el-button
        >
      </template>
      <template v-slot:status="{ row }">
        <el-tag :type="tagArr[row.status]">{{ statusArr[row.status] }}</el-tag>
      </template>
    </avue-crud>
  </basic-container>
</template>

<script>
import { getDoctorList, approve, reject } from '@/api/doctor/index.js'
import { baseUrl } from '@/api/baseUrl'
export default {
  data() {
    return {
      data: [],
      form: {},
      page: {
        pageSize: 10,
        currentPage: 1,
        total: 0
      },
      currentTab: 'pending',
      tabOption: {
        column: [
          {
            // icon: 'el-icon-info',
            label: '待审核',
            prop: 'pending'
          },
          {
            // icon: 'el-icon-warning',
            label: '审核通过',
            prop: 'approved'
          },
          {
            // icon: 'el-icon-warning',
            label: '审核不通过',
            prop: 'rejected'
          }
        ]
      },
      tagArr: { pending: 'info', approved: 'success', rejected: 'error' },
      statusArr: {
        pending: '待审核',
        approved: '审核通过',
        rejected: '审核不通过'
      },
      option: {
        tip: false,
        simplePage: true,
        searchShow: true,
        searchMenuSpan: 18,
        searchMenuPosition: 'right',
        tree: true,
        border: true,
        addBtn: false,
        index: true,
        // selection: true,
        editBtn: false,
        delBtn: false,
        viewBtn: false,
        menuWidth: 300,
        dialogClickModal: false,
        height: 700,
        addBtn: true,
        delBtn: false,
        labelWidth: 120,
        column: [
          {
            label: '医生姓名',
            prop: 'name'
          },
          {
            label: '学历',
            prop: 'education'
          },
          {
            label: '毕业院校',
            prop: 'graduationSchool'
          },
          {
            label: '证书名称',
            prop: 'certificateName'
          },
          {
            label: '证书',
            prop: 'certificateFile',
            type: 'upload'
            // display: false
          },
          {
            label: '审核状态',
            prop: 'status',
            type: 'select',
            // search: true,
            slot: true,
            dicData: [
              {
                label: '待审核',
                value: 'pending'
              },
              {
                label: '审核通过',
                value: 'approved'
              },
              {
                label: '审核不通过',
                value: 'rejected'
              }
            ]
          }
        ]
      }
    }
  },
  components: {},
  created() {},
  mounted() {},
  computed: {},
  watch: {},
  methods: {
    check(row) {
      this.$confirm('确定审核通过？', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        approve(row.id).then((res) => {
          this.searchReset()
        })
      })
    },
    rejectApply(row) {
      this.$confirm('确定驳回该申请？', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        reject(row.id).then((res) => {
          this.searchReset()
        })
      })
    },
    downloadFile(url) {
      this.$preView('http://localhost:3000/' + url)
    },
    uploadAfter(res, done) {
      console.log(res, '????????????')
      // this.form.pastPapersDownload = res.data.fileUrl
      done()
    },
    setParams(row) {
      let {
        title,
        examIntroduction,
        registrationTime,
        examSchedule,
        examSyllabus,
        medicalExamQa,
        pastPapersDownload
      } = row
      const regTime = JSON.stringify(registrationTime)
      const examTime = JSON.stringify(examSchedule)
      // const files = JSON.stringify(pastPapersDownload)
      const params = {
        title,
        examIntroduction,
        registrationTime: regTime,
        examSchedule: examTime,
        examSyllabus,
        medicalExamQa,
        pastPapersDownload
      }
      return params
    },
    rowSave(row, done, loading) {
      const params = this.setParams(row)
      console.log(params)
      addCer(params).then(
        () => {
          this.onLoad(this.page)
          this.$message({
            type: 'success',
            message: '操作成功!'
          })
          done()
        },
        (error) => {
          window.console.log(error)
          loading()
        }
      )
    },
    tabChange(column) {
      this.currentTab = column.prop
      this.page.currentPage = 1
      this.searchReset()
    },
    onLoad(page, params = {}) {
      this.loading = true
      getDoctorList(page.currentPage, page.pageSize, {
        status: this.currentTab
      }).then((res) => {
        const data = res.data.data
        this.page.total = res.data.total
        this.data = data
        console.log(res, this.data, res.data, '...............')
        this.loading = false
      })
    },
    currentChange(currentPage) {
      this.page.currentPage = currentPage
      // this.onLoad(this.page)
    },
    sizeChange(pageSize) {
      this.page.pageSize = pageSize
    },
    refreshChange() {
      this.onLoad(this.page, this.query)
    },
    searchReset() {
      this.query = {}
      this.onLoad(this.page)
    },
    searchChange(params, done) {
      this.query = params
      this.page.currentPage = 1
      this.onLoad(this.page, this.query)
      done()
    }
  }
}
</script>
